﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "LPHeaderControl.h"
#include "Controls/LPHardWare/LPMemroyManager.h"
#include "Controls/LPSwitchStyle/LPSwitchStyleWidget.h"
#include "Controls/LPProcessEllipse/LPProcessEllipse.h"

#include <QDir>
#include <QComboBox>
#include <QMouseEvent>
#include <QStyleFactory>
#include <QDesktopWidget>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setWindowFlags(Qt::FramelessWindowHint);

    m_pHeaderControl = new LPHeaderControl( this );
    m_pHeaderControl->SetAppName(u8"小鹏Demo");
    m_pHardWareMointor = new LPMemroyManager();
    m_pSwitchWidget = new LPSwitchStyleWidget();
    InitStyle();
    InitActionItems();
    InitControlWidget();
    showMaximized();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::InitStyle()
{
    QString strDir = QDir::currentPath();
    QFile file( strDir + "/Theme/qss/Maindows.qss" );
    if( file.open(QFile::ReadOnly) )
    {
        QString strStyleSheet = file.readAll();
        setStyleSheet( strStyleSheet );
        file.close();
    }
}

void MainWindow::InitControlWidget()
{
    ui->widget_1->startTimer();
    ui->widget_2->setRingColor(QColor(155,155,0));
    ui->widget_2->setCircleColor(QColor(0,255,170));
    ui->widget_2->setTextColor(QColor(0,0,0));
    ui->widget_2->setRingPadding(10);
    ui->widget_2->setRingWidth(5);
    ui->widget_2->setBackgroundColor(QColor(0,255,0));
    ui->widget_2->startTimer();

    ui->widget_3->setRingColor(QColor(155,0,0));
    ui->widget_3->setCircleColor(QColor(255,255,0));
    ui->widget_3->setTextColor(QColor(255,120,0));
    ui->widget_3->setRingPadding(20);
    ui->widget_3->setRingWidth(20);
    ui->widget_3->setBackgroundColor(QColor(51,235,255));

    ui->widget_3->startTimer();
    ui->widget_4->startTimer();
}

void MainWindow::InitActionItems()
{
    if( LPMenuPage* page = m_pHeaderControl->AddMenuGroup(u8"菜单栏") )
    {
        if( QToolButton * toolbutton  = m_pHeaderControl->AddAction(page,0,0,u8"内存查看") )
        {
            toolbutton->setIcon(QIcon(":/Theme/Images/MenuItems/memory.png"));
            connect(toolbutton,&QToolButton::clicked,[=]()
            {
                if( m_pHardWareMointor->isHidden())
                {
                    m_pHardWareMointor->show();
                }
            });
        }
    }
    if( LPMenuPage* page = m_pHeaderControl->AddMenuGroup(u8"菜单栏") )
    {
        if( QToolButton * toolbutton  = m_pHeaderControl->AddAction(page,0,1,u8"风格切换") )
        {
            toolbutton->setIcon(QIcon(":/Theme/Images/MenuItems/theme.png"));
            connect(toolbutton,&QToolButton::clicked,[=]()
            {
                m_pSwitchWidget->show();
            });
        }
    }
    for( int i=2; i<25; i++ )
    {
        if( LPMenuPage* page = m_pHeaderControl->AddMenuGroup(u8"菜单栏") )
        {
            if( QToolButton * toolbutton  = m_pHeaderControl->AddAction(page,0,i,QString(u8"菜单%1").arg(i)) )
            {
                toolbutton->setIcon(QIcon(":/Theme/Images/MenuItems/mapIcon.png"));
            }
        }
    }
}


void MainWindow::InitConnection()
{
#if (QT_VERSION > QT_VERSION_CHECK(5,0,0))
    connect(m_pHeaderControl,&LPHeaderControl::heightChange,this,&MainWindow::AdjuestSize);

#elif
    connect(m_pHeaderControl,SIGNAL(heightChange()),this,SLOT(AdjuestSize()));
#endif
}

void MainWindow::resizeEvent( QResizeEvent *event )
{
    Q_UNUSED(event);
    m_pHeaderControl->setFixedWidth(this->width());
    AdjuestSize();
}

void MainWindow::showEvent(QShowEvent *event)
{
    Q_UNUSED(event);
    AdjuestSize();
}

void MainWindow::AdjuestSize()
{
    int width = QApplication::desktop()->geometry().width();
    int heigth = QApplication::desktop()->geometry().height();
    ui->widget->setGeometry(0,m_pHeaderControl->height(),width,heigth-m_pHeaderControl->height()-90);
}

